package tptecnicas.printers;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import javax.xml.stream.XMLStreamException;

import tptecnicas.configuration.XmlReader;

/**
 * Implementacion especifica de escritor de logs
 * para proveer el soporte a escritores personalizados.
 * Se compone del objeto que implementa la escritura particular.
 */
public class CustomWriter extends Writer{

	private Object customWriter;
	private List<String> parameters; //first parameter is the class name
	
	public CustomWriter(List<String> list) {
		super(list);
		//parse custom writer structure ...
		XmlReader reader = new XmlReader();
		try {
			FileInputStream inputStream = new FileInputStream(path);
			parameters = reader.readCustomXMLFile(inputStream);
		} catch (FileNotFoundException e) {
			System.err.println("File" + path + "not found");
		} catch (XMLStreamException e) {
			System.err.println("XMLStreamException: " + e.getMessage());
		}
		try {
			Class customClass = Class.forName(parameters.get(0));
			Constructor constructor = customClass.getConstructor(new Class[]{List.class});
			customWriter = constructor.newInstance(parameters.subList(1, parameters.size()));
		} catch (IllegalArgumentException e) {
			System.err.println("IllegalArgumentException: " + e.getMessage());
		} catch (InvocationTargetException e) {
			System.err.println("InvocationTargetException: " + e.getMessage());
		} catch (ClassNotFoundException e) {
			System.err.println("ClassNotFoundException: " + e.getMessage());
		} catch (InstantiationException e) {
			System.err.println("InstantiationException: " + e.getMessage());
		} catch (IllegalAccessException e) {
			System.err.println("IllegalAccessException: " + e.getMessage());
		} catch (NoSuchMethodException e) {
			System.err.println("NoSuchMethodException: " + e.getMessage());
		} catch (SecurityException e) {
			System.err.println("SecurityException: " + e.getMessage());
		}
	}

	@Override
	public void writeOnOutput(String result) {
		try {
			Method method = customWriter.getClass().getMethod("writeOnOutput", new Class[]{String.class});
			method.invoke(customWriter, result);
		} catch (NoSuchMethodException | SecurityException e) {
			System.err.println("Unable to write with writer " + this.path + 
					": writeOnOutput method is undeclared. " + e.getMessage());
		} catch (IllegalAccessException e) {
			System.err.println("IllegalAccessException: " + e.getMessage());
		} catch (IllegalArgumentException e) {
			System.err.println("IllegalArgumentException: " + e.getMessage());
		} catch (InvocationTargetException e) {
			System.err.println("InvocationTargetException: " + e.getMessage());
		}
	}
	
	@Override
	public String getPath() {
		return this.getPath();
	}
	
	@Override
	public Level getLevel() {
		return this.getLevel();
	}
}
